package ee.subjects.frontend.controller;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ViewManager {
	
	private String mode;
	private String event;
	private HttpServletRequest request;
	private HttpServletResponse response;
	private ServletContext context; 
	
	ViewManager(String mode, String event, HttpServletRequest request, HttpServletResponse response, ServletContext context){
		this.mode=mode;
		this.event=event;
		
		this.request=request;
		this.response=response;
		this.context=context;
	}

	public void navigate() {
		int user_view = 0;
		
		try{
			if(mode.equals("person")){
				if(event.equals("insert_person") || event.equals("edit_person")){
					user_view=1;
				}
				if(event.equals("allPersons")){
					user_view=7;
				}
			}else if(mode.equals("enterprise")){
				if(event.equals("insert_enterprise")|| event.equals("edit_enterprise")){
					user_view=2;
				}
				if(event.equals("allEnterprises")){
					user_view=8;
				}
			}else if(mode.equals("employee")){
				if(event.equals("insert_employee")){
					user_view=3;
				}
				if(event.equals("enterprise_list_for_employees")){
					user_view=4;
				}
				if(event.equals("roles")){
					user_view=5;
				}
			}else if(mode.equals("undefined")&&event.equals("undefined")){
				user_view=6;
			}
			
		}catch(Exception ex){
			//TODO Logimine
		}
		
		this.doSwitch(user_view);
	}

	private void doSwitch(int user_view) {
		try{
			switch (user_view) {
	
			case 1 :  
				context.getRequestDispatcher("/personForm.jsp").forward(request, response);
				break ;
				
			case 2 :
				context.getRequestDispatcher("/enterpriseForm.jsp").forward(request, response);
				break;
				
			case 3 :
				context.getRequestDispatcher("/insertEmployeeForm.jsp").forward(request, response);
				break;
				
			case 4 :
				context.getRequestDispatcher("/enterpriseListForEmployees.jsp").forward(request, response);
				break;
			
			case 5 :
				context.getRequestDispatcher("/employeeRoles.jsp").forward(request, response);
				break;
				
			case 6 :
				context.getRequestDispatcher("/firstPage.jsp").forward(request, response);
				break;
				
			case 7 :
				context.getRequestDispatcher("/allPersons.jsp").forward(request, response);
				break;
				
			case 8 :
				context.getRequestDispatcher("/allEnterprises.jsp").forward(request, response);
				break;
				
				
				
			default: 
				// Do nothing for AJAX requests
				break ;
	
			}
		}catch(Exception ex){
			//TODO Logimine
		}
	}
}
